package com.rovinj_server.ui;

import android.app.ProgressDialog;
import android.os.AsyncTask;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.ActionBar;
import android.support.v7.app.AppCompatActivity;
import android.text.TextUtils;
import android.widget.Button;
import android.widget.RelativeLayout;
import android.widget.Toast;

import com.rovinj_server.R;
import com.rovinj_server.adapter.PeopleRecyclerAdapter;
import com.rovinj_server.db.DBManager;
import com.rovinj_server.entity.People;
import com.rovinj_server.utisl.Directory;
import com.rovinj_server.utisl.ExcelUtils;

import java.io.File;
import java.io.IOException;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;
import jxl.write.WriteException;


/**
 * Created by root on 8/13/16.
 */
public class ExportActivity extends AppCompatActivity {

    @Bind(R.id.fh)
    RelativeLayout mFh;
    @Bind(R.id.fah_jl)
    RelativeLayout mFahJl;
    @Bind(R.id.rl_return)
    RelativeLayout mRlReturn;
    @Bind(R.id.butt_dao_chu)
    Button mButtDaoChu;
    public  static final  String TAG="-----ExportActivity";
    private PeopleRecyclerAdapter peopleRecyclerAdapter;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_dao_chu);
        ButterKnife.bind(this);
        hidStater();
        peopleRecyclerAdapter = new PeopleRecyclerAdapter();
        peopleRecyclerAdapter.add(
                new People("Agi Maulana", "Mahasiswa", "Mahasiswa",  "085783834545", "aggysuck@gmail.com", "Bogor"));
        peopleRecyclerAdapter.add(
                new People("Asep Surasep", "Montir", "Mahasiswa",  "087349830298", "asepsur@gmail.com", "Dayeuhkolot, Bandung"));
        peopleRecyclerAdapter.add(
                new People("Sanusi", "Pedagang Buah", "Mahasiswa",  "0878734546323", "sun.oosy@gmail.com", "Cikarang, Bekasi"));
        peopleRecyclerAdapter.add(
                new People("Zaenal Abidin", "Mahasiswa", "Mahasiswa",  "085786475928", "zabidin@gmail.com", "Ciawi, Bogor"));
        peopleRecyclerAdapter.add(
                new People("Paijo", "Manager", "Mahasiswa",  "0857462883749", "paijo.mgr@gmail.com", "Surabaya"));
        //RecyclerView recyclerView = (RecyclerView) findViewById(R.id.recyclerview);
        //recyclerView.setLayoutManager(new LinearLayoutManager(this));
        // recyclerView.setAdapter(peopleRecyclerAdapter);
    }
    /**
     * 隐藏状态栏
     */
    private void hidStater() {
        ActionBar actionBar = getSupportActionBar();
        actionBar.hide();
    }

    private void export() {
        final ProgressDialog progressDialog = new ProgressDialog(this);
        progressDialog.setCancelable(false);
        progressDialog.setIndeterminate(true);
        progressDialog.show();

        new AsyncTask<Void, Void, String>() {
            @Override
            protected String doInBackground(Void... params) {
                File file = Directory.getExcelFile();

                try {
                    ExcelUtils.write(file, peopleRecyclerAdapter.getPeoples());
                } catch (final IOException e) {
                    e.printStackTrace();

                    runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            Toast.makeText(ExportActivity.this, "Error : " + e.getMessage(), Toast.LENGTH_SHORT).show();
                        }
                    });


                    return null;
                } catch (final WriteException e) {
                    e.printStackTrace();
                    runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            Toast.makeText(ExportActivity.this, "Error : " + e.getMessage(), Toast.LENGTH_SHORT).show();
                        }
                    });
                    return null;
                }
                return file.getAbsolutePath();
            }

            @Override
            protected void onPostExecute(String path) {
                progressDialog.dismiss();
                if (TextUtils.isEmpty(path))
                    Toast.makeText(ExportActivity.this, "Gagal mengekspor", Toast.LENGTH_SHORT).show();
                else
                    Toast.makeText(ExportActivity.this, "Berhasil mengekspor" + path, Toast.LENGTH_SHORT).show();
            }
        }.execute();
    }

    @OnClick(R.id.butt_dao_chu)
    public void onClick() {
      // export();
        DBManager dbManager = new DBManager(this);
        dbManager.copyDBFile();
        int num = dbManager.dedeteData(3);
        Toast.makeText(this,"清除成功，共清除"+num+"条数据",Toast.LENGTH_LONG).show();
    }
}
